﻿using AutoMapper;
using B.Z.BasicData.Infrastructure;
using B.Z.Inventory.API.Write.Application.Command;
using B.Z.Inventory.Domain;
using B.Z.Inventory.ErrorCode;
using MediatR;

namespace B.Z.Inventory.API.Write.Application.CommandHandler
{
    public class InventoryTaskAddCommandHandler : IRequestHandler<InventoryTaskAddCommand, APIResult<int>>
    {
        private readonly IBaseRepository<T_InventoryTask> inventoryTaskRepository;
        private readonly IMapper mapper;
        public InventoryTaskAddCommandHandler(IBaseRepository<T_InventoryTask> inventoryTaskRepository, IMapper mapper)
        {
            this.inventoryTaskRepository = inventoryTaskRepository;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(InventoryTaskAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code = APIEnums.ok;

            result.Message = "添加成功";
            var model = mapper.Map<T_InventoryTask>(request);
            result.Data =await inventoryTaskRepository.Add(model);
            return Task.FromResult(result).Result;
        }
    }
}
